/* $Header: leds.h,v 2.2 01/10/02 10:30:28 przybyls Exp $ */

/***********************************************************************/
/* INCLUDE FILE NAME:  leds.h                                          */
/* ==================                                                  */
/*                                                                     */
/* Author:  Lesley Freed                                               */
/*                                                                     */
/* COMMENT:  This include file contains types and defines used by      */
/*           the led control.                                          */
/*                                                                     */
/*                                                                     */
/*                                                                     */
/***********************************************************************/

#ifndef LEDS_H
#define LEDS_H

/*****************************/
/*   EXTERNALS               */
/*****************************/

extern  U_INT8  LED_Driver_Count;       /* counter for 10.4 Hz LED Driver     */
extern  U_INT8  LED_Blink_Count;        /* counter for LED blinking           */

/*****************************/
/*   SYMBOLIC CONSTANTS      */
/*****************************/


/*****************************/
/*   MACROS                  */
/*****************************/

#define LED_CONTROL_RATE 40
#define LED_BLINK_RATE 7
#define LED_BLINK_DUTY_CYCLE 3

#define TURN_DEVIATION_LED_ON()                                         \
                     {                                                  \
                        INTERRUPTS_OFF                                  \
                        LED2_Image |= DEVIATION_LED_ON;                 \
                        INTERRUPTS_ON                                   \
                     }

#define TURN_DEVIATION_LED_OFF()                                        \
                     {                                                  \
                        INTERRUPTS_OFF                                  \
                        LED2_Image &= DEVIATION_LED_OFF;                \
                        INTERRUPTS_ON                                   \
                     }

/* returns 0 = off, 1 = on, 2 = blink */
#define GET_DEVIATION_LED()						\
	((LED2_Blink_Image & DEVIATION_LED_ON) ? 2 :			\
	 ((LED2_Image & DEVIATION_LED_ON) ? 1 : 0))

#define TURN_NOT_READY_LED_ON()                                         \
                     {                                                  \
                        INTERRUPTS_OFF                                  \
                        LED2_Image |= NOT_RDY_LED_ON;                   \
                        LED2_Blink_Image &= NOT_RDY_LED_OFF;            \
                        INTERRUPTS_ON                                   \
                     }

#define BLINK_NOT_READY_LED()                                           \
                     {                                                  \
                        INTERRUPTS_OFF                                  \
                        LED2_Image |= NOT_RDY_LED_ON;                   \
                        LED2_Blink_Image |= NOT_RDY_LED_ON;             \
                        INTERRUPTS_ON                                   \
                     }

#define TURN_NOT_READY_LED_OFF()                                        \
                     {                                                  \
                        INTERRUPTS_OFF                                  \
                        LED2_Image &= NOT_RDY_LED_OFF;                  \
                        LED2_Blink_Image &= NOT_RDY_LED_OFF;            \
                        INTERRUPTS_ON                                   \
                     }

/* returns 0 = off, 1 = on, 2 = blink */
#define GET_NOT_READY_LED()						\
	((LED2_Blink_Image & NOT_RDY_LED_ON) ? 2 :			\
	 ((LED2_Image & NOT_RDY_LED_ON) ? 1 : 0))

#define TURN_INIT_TIME_LED_ON()                                         \
                     {                                                  \
                        INTERRUPTS_OFF                                  \
                        LED1_Image |= INIT_LED_ON;                      \
                        INTERRUPTS_ON                                   \
                     }

/* returns 0 = off, 1 = on, 2 = blink */
#define GET_INIT_TIME_LED()						\
	((LED1_Blink_Image & INIT_LED_ON) ? 2 :				\
	 ((LED1_Image & INIT_LED_ON) ? 1 : 0))

#define TURN_RAMP_LED_ON()                                              \
                     {                                                  \
                        INTERRUPTS_OFF                                  \
                        LED1_Image &= INIT_LED_OFF & FINAL_LED_OFF;     \
                        LED1_Image |= RAMP_LED_ON;                      \
                        INTERRUPTS_ON                                   \
                     }

#define BLINK_RAMP_LED()                                                \
                     {                                                  \
                        INTERRUPTS_OFF                                  \
                        LED1_Blink_Image |= RAMP_LED_ON;                \
                        INTERRUPTS_ON                                   \
                     }

#define BLINK_RAMP_LED_OFF()                                            \
                     {                                                  \
                        INTERRUPTS_OFF                                  \
                        LED1_Blink_Image &= RAMP_LED_OFF;               \
                        INTERRUPTS_ON                                   \
                     }

/* returns 0 = off, 1 = on, 2 = blink */
#define GET_RAMP_LED()							\
	((LED1_Blink_Image & RAMP_LED_ON) ? 2 :				\
	 ((LED1_Image & RAMP_LED_ON) ? 1 : 0))

#define TURN_FINAL_LED_ON()                                             \
                     {                                                  \
                        INTERRUPTS_OFF                                  \
                        LED1_Image &= RAMP_LED_OFF;                     \
                        LED1_Blink_Image &= RAMP_LED_OFF;               \
                        LED1_Image |= FINAL_LED_ON;                     \
                        INTERRUPTS_ON                                   \
                     }

/* returns 0 = off, 1 = on, 2 = blink */
#define GET_FINAL_LED()							\
	((LED1_Blink_Image & FINAL_LED_ON) ? 2 :			\
	 ((LED1_Image & FINAL_LED_ON) ? 1 : 0))

/* Note:  RunLedOn should only be called from the StartRun interrupt,  */
/*        because of this it does not turn interrupts off.             */

#define TURN_RUN_LED_ON()                                               \
                     {                                                  \
                        LED1_Image |= RUN_LED_ON;                       \
                        LED1_Blink_Image &= RUN_LED_OFF;                \
                     }

#define TURN_RUN_LED_OFF()                                              \
                     {                                                  \
                        INTERRUPTS_OFF                                  \
                        LED1_Image &= (FINAL_LED_OFF &                  \
                                            RAMP_LED_OFF &              \
                                            INIT_LED_OFF &              \
                                            RUN_LED_OFF);               \
                        LED1_Blink_Image &= (RAMP_LED_OFF &             \
                                            RUN_LED_OFF);               \
                        INTERRUPTS_ON                                   \
                     }


#define BLINK_RUN_LED()                                                 \
                     {                                                  \
                        INTERRUPTS_OFF                                  \
                        LED1_Image &= (FINAL_LED_OFF &                  \
                                            RAMP_LED_OFF &              \
                                            INIT_LED_OFF);              \
                        LED1_Blink_Image &= RAMP_LED_OFF;               \
                        LED1_Image |= RUN_LED_ON;                       \
                        LED1_Blink_Image |= RUN_LED_ON;                 \
                        INTERRUPTS_ON                                   \
                     }

/* returns 0 = off, 1 = on, 2 = blink */
#define GET_RUN_LED()							\
	((LED1_Blink_Image & RUN_LED_ON) ? 2 :				\
	 ((LED1_Image & RUN_LED_ON) ? 1 : 0))

#define TURN_REMOTE_LED_ON()                                            \
                     {                                                  \
                        INTERRUPTS_OFF                                  \
                        LED2_Image |= REMOTE_LED_ON;                    \
                        LED2_Blink_Image &= REMOTE_LED_OFF;             \
                        INTERRUPTS_ON                                   \
                     }

#define TURN_REMOTE_LED_OFF()                                           \
                     {                                                  \
                        INTERRUPTS_OFF                                  \
                        LED2_Image &= REMOTE_LED_OFF;                   \
                        LED2_Blink_Image &= REMOTE_LED_OFF;             \
                        INTERRUPTS_ON                                   \
                     }

#define BLINK_REMOTE_LED()                                              \
                     {                                                  \
                        INTERRUPTS_OFF                                  \
                        LED2_Image |= REMOTE_LED_ON;                    \
                        LED2_Blink_Image |= REMOTE_LED_ON;              \
                        INTERRUPTS_ON                                   \
                     }

/* returns 0 = off, 1 = on, 2 = blink */
#define GET_REMOTE_LED()						\
	((LED2_Blink_Image & REMOTE_LED_ON) ? 2 :			\
	 ((LED2_Image & REMOTE_LED_ON) ? 1 : 0))

#define TURN_CLOCK_TBL_LED_ON()                                         \
                     {                                                  \
                        INTERRUPTS_OFF                                  \
                        LED2_Image |= CLOCK_TBL_LED_ON;                 \
                        INTERRUPTS_ON                                   \
                     }

#define TURN_CLOCK_TBL_LED_OFF()                                        \
                     {                                                  \
                        INTERRUPTS_OFF                                  \
                        LED2_Image &= CLOCK_TBL_LED_OFF;                \
                        INTERRUPTS_ON                                   \
                     }

/* returns 0 = off, 1 = on, 2 = blink */
#define GET_CLOCK_TBL_LED()						\
	((LED2_Blink_Image & CLOCK_TBL_LED_ON) ? 2 :			\
	 ((LED2_Image & CLOCK_TBL_LED_ON) ? 1 : 0))

#define TURN_FRONT_MISER_LED_OFF                                        \
                        {                                               \
                        INTERRUPTS_OFF                                  \
                        LED2_Image &= FRONT_MISER_LED_OFF;              \
                        INTERRUPTS_ON                                   \
                        }

#define TURN_FRONT_MISER_LED_ON                                         \
                        {                                               \
                        INTERRUPTS_OFF                                  \
                        LED2_Image |= FRONT_MISER_LED_ON;               \
                        INTERRUPTS_ON                                   \
                        }

/* returns 0 = off, 1 = on, 2 = blink */
#define GET_FRONT_MISER_LED()						\
	((LED2_Blink_Image & FRONT_MISER_LED_ON) ? 2 :			\
	 ((LED2_Image & FRONT_MISER_LED_ON) ? 1 : 0))

#define TURN_BACK_MISER_LED_OFF                                         \
                        {                                               \
                        INTERRUPTS_OFF                                  \
                        LED2_Image &= BACK_MISER_LED_OFF;               \
                        INTERRUPTS_ON                                   \
                        }

#define TURN_BACK_MISER_LED_ON                                          \
                        {                                               \
                        INTERRUPTS_OFF                                  \
                        LED2_Image |= BACK_MISER_LED_ON;                \
                        INTERRUPTS_ON                                   \
                        }

/* returns 0 = off, 1 = on, 2 = blink */
#define GET_BACK_MISER_LED()						\
	((LED2_Blink_Image & BACK_MISER_LED_ON) ? 2 :			\
	 ((LED2_Image & BACK_MISER_LED_ON) ? 1 : 0))

#define TURN_PREP_RUN_LED_ON()                                          \
                     {                                                  \
                        INTERRUPTS_OFF                                  \
                        LED2_Image |= PREP_RUN_LED_ON;                  \
                        LED2_Blink_Image &= PREP_RUN_LED_OFF;           \
                        INTERRUPTS_ON                                   \
                     }

#define TURN_PREP_RUN_LED_OFF()                                         \
                     {                                                  \
                        INTERRUPTS_OFF                                  \
                        LED2_Image &= PREP_RUN_LED_OFF;                 \
                        LED2_Blink_Image &= PREP_RUN_LED_OFF;           \
                        INTERRUPTS_ON                                   \
                     }

#define BLINK_PREP_RUN_LED()                                            \
                     {                                                  \
                        INTERRUPTS_OFF                                  \
                        LED2_Image |= PREP_RUN_LED_ON;                  \
                        LED2_Blink_Image |= PREP_RUN_LED_ON;            \
                        INTERRUPTS_ON                                   \
                     }

/* returns 0 = off, 1 = on, 2 = blink */
#define GET_PREP_RUN_LED()						\
	((LED2_Blink_Image & PREP_RUN_LED_ON) ? 2 :			\
	 ((LED2_Image & PREP_RUN_LED_ON) ? 1 : 0))

#define TURN_POST_RUN_LED_ON()                                          \
                     {                                                  \
                        INTERRUPTS_OFF                                  \
                        LED2_Image |= POST_RUN_LED_ON;                  \
                        LED2_Blink_Image &= PREP_RUN_LED_OFF;           \
                        INTERRUPTS_ON                                   \
                     }

#define TURN_POST_RUN_LED_OFF()                                         \
                     {                                                  \
                        INTERRUPTS_OFF                                  \
                        LED2_Image &= POST_RUN_LED_OFF;                 \
                        LED2_Blink_Image &= POST_RUN_LED_OFF;           \
                        INTERRUPTS_ON                                   \
                     }

#define BLINK_POST_RUN_LED()                                            \
                     {                                                  \
                        INTERRUPTS_OFF                                  \
                        LED2_Image |= POST_RUN_LED_ON;                  \
                        LED2_Blink_Image |= POST_RUN_LED_ON;            \
                        INTERRUPTS_ON                                   \
                     }

/* returns 0 = off, 1 = on, 2 = blink */
#define GET_POST_RUN_LED()						\
	((LED2_Blink_Image & POST_RUN_LED_ON) ? 2 :			\
	 ((LED2_Image & POST_RUN_LED_ON) ? 1 : 0))

/*****************************/
/*   TYPEDEFS                */
/*****************************/

        enum    BLINK_MODE { BLINK_OFF = 0,
                             BLINK_ON  = 1
                           };

/*****************************/
/*   FUNCTION PROTOTYPES     */
/*****************************/

#ifndef mccabe
#pragma SECTION PROG=boot
#endif
        void InitializeLEDs( void);
        void TurnAllLedsOn( void);
#ifndef mccabe
#pragma SECTION UNDO
#endif

#endif
